package com.controller;

/**
 * @ClassName: AreaController
 * @Description:
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2021/6/26 0026 上午 11:02
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2021/6/26 0026        Fu Hao      v1.0.0         创建
 */
import cn.hutool.core.util.StrUtil;
import com.pojo.Area;
import com.service.AreaService;
import com.util.AreaUtils;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("area")
//@Api(value = "获取行政区划", tags = "获取行政区划数据")
@Slf4j
public class AreaController {
    /**
     * 服务对象
     */
    @Resource
    private AreaService areaService;

    @Resource
    private AreaUtils areaUtils;

    /**
     * 标识符
     */
    private static final String NONE = "[]";

    private static final String STREET = "street";

    private static final String PROVINCE = "province";

    /**爬取数据：1
     * 只保存省级行政单位下3级的数据
     */
    @GetMapping(value = "/getActive")
    //@ApiOperation(value = "只保存省级行政单位下3级的数据", notes = "只保存省级行政单位下3级的数据")
    public void getActive() {
        // 获取国家节点数据
        List<Area> countryList = areaUtils.getInfo("", "1");
        countryList.forEach(country -> {
            // 获取国家下面的所有省份
            List<Area> provinceList = country.getDistricts();
            // 排序
            provinceList = provinceList.stream().sorted(Comparator.comparing(Area::getAdCode)).collect(Collectors.toList());
            provinceList.forEach(province -> {
                List<Area> cityList = areaUtils.getInfo(province.getAdCode(), "3");
                recursion(cityList, province.getAdCode());
            });
        });
        log.info("数据拉取保存成功!");
    }

    /**
     * 递归保存数据
     *
     * @param parentList 父级行政单位数据
     */
    public void recursion(List<Area> parentList, String parentCode) {
        if (CollectionUtils.isEmpty(parentList)) {
            return;
        }
        // 对街道,乡镇adCode特殊处理
        for (int i = 0; i < parentList.size(); i++) {
            Area area = parentList.get(i);
            if (StrUtil.equals(NONE, area.getCityCode())) {
                area.setCityCode("");
            }
            if (StrUtil.equals(STREET, area.getLevel())) {
                Long streetId = Long.valueOf(area.getAdCode().concat(String.format("%02d", i + 1)));
                area.setAdCode(String.valueOf(streetId));
            }
        }
        parentList.forEach(child -> {
            // 设置父id
            if (StrUtil.equals(PROVINCE, child.getLevel())) {
                child.setParentCode(String.valueOf(0));
            } else {
                child.setParentCode(parentCode);
            }
            // 保存数据
            if (areaService.save(child)) {
                if (!CollectionUtils.isEmpty(child.getDistricts())) {
                    recursion(child.getDistricts(), child.getAdCode());
                }
            }
        });

    }
}
